
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

from ui.axis import Ui_Form
from widgets.axis.axis_select_positions_dialog import AxisSelectPositionsDialog

from common.libs.logger import logger

import qtawesome as qta

class AxisWidget(QWidget, Ui_Form):

    online_status = {0: "离线", 1: "在线"}

    def __init__(self):
        super(AxisWidget, self).__init__()
        self.setupUi(self)
        self.pushButton_axis_0_set.clicked.connect(lambda: self.setPositions(0))
        self.pushButton_axis_1_set.clicked.connect(lambda: self.setPositions(1))
        self.pushButton_axis_2_set.clicked.connect(lambda: self.setPositions(2))
        self.pushButton_axis_3_set.clicked.connect(lambda: self.setPositions(3))


    def setDevice(self, device):
        self.device = device
        self.updateInfo()

    def setOnline(self, online):
        if online == 1:
            self.label_online.setText(chr(0xf192) + ' ' + '在线')
            self.label_online.setFont(qta.font('fa', 16))
            self.label_online.setStyleSheet("color:#00ff00;")
        else:
            self.label_online.setText(chr(0xf192) + ' ' + '离线')
            self.label_online.setFont(qta.font('fa', 16))
            self.label_online.setStyleSheet("color:#ff0000;")

    def updateInfo(self):
        device = self.device
        self.groupBox_device.setTitle(device['name'] + '-' + device['position'])
        self.setOnline(device['online'])

        for axis in device['axis']:
            if axis['id'] == 0:
                self.groupBox_axis_0.setTitle(axis['name'])
            if axis['id'] == 1:
                self.groupBox_axis_1.setTitle(axis['name'])
            if axis['id'] == 2:
                self.groupBox_axis_2.setTitle(axis['name'])
            if axis['id'] == 3:
                self.groupBox_axis_3.setTitle(axis['name'])

    def setPositions(self, num):
        select_dlg = AxisSelectPositionsDialog()
        axis = self.device['axis']
        for a in axis:
            if a['id'] == num:
                select_dlg.setPositions(a['positions'])
                if select_dlg.exec() == QDialog.Accepted:
                    position = select_dlg.position
                    if num == 0:
                        self.doubleSpinBox_axis_0_target.setValue(float(position))
                    if num == 1:
                        self.doubleSpinBox_axis_1_target.setValue(float(position))
                    if num == 2:
                        self.doubleSpinBox_axis_2_target.setValue(float(position))
                    if num == 3:
                        self.doubleSpinBox_axis_3_target.setValue(float(position))
                break
    